home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tpsecure.zip / SECURE.PAS next >
Pascal/Delphi Source File  |  1992-03-11  |  10KB  |  269 lines

  1. unit Secure;
  2.  
  3. {===========================================================================+
  4. | File:         Secure.PAS                                                     |
  5. | Project:      Security for any Turbo Pascal Program                       |
  6. +---------------------------------------------------------------------------+
  7. | VER   DATE      AUTHOR - VERSION HISTORY, DESCRIPTION, or NOTES           |
  8. +---------------------------------------------------------------------------+
  9.  
  10.   1.0   03/09/92  Jason Weaver : Original Version
  11.  
  12. ----- DOCUMENTATION ---------------------------------------------------------
  13.  
  14. Development language/tools/libs:  Turbo Pascal 6.0
  15.  
  16. Intructions and Notes:
  17.    This tiny unit is designed to illustarte how a secrity can be easily added
  18.    to any turbo pascal program.  This secruity is by no means unbreakable, but
  19.    it will cause alot of pain and frustrarion on the part of hackers to figure
  20.    out.  This unit could be extended by adding encription or other "deterants".
  21.  
  22.    I claim no responsibility for any screw ups encountered by using this unit.
  23.    As of 03/09/92 it worked great.  For more information, see the module
  24.    headers and TEST.PAS program.
  25.  
  26. ===== END OF FILE HEADER ===================================================}
  27. {$I-}
  28.  
  29. Interface
  30.  
  31. Uses DOS;
  32.  
  33. Type  TStr12     = String[12];
  34.       TSecureRec = Record                { RECORD STORED IN THE HIDDEN FILE }
  35.         CurrCount : Word;                { COUNTER FOR NUMBER TIMES RUN     }
  36.         MaxCount  : Word;                { MAXIMUM ALLOWABLE TIME TO RUN    }
  37.         EXE_Time  : LongInt;             { TIME EXE FILE CREATED            }
  38.         EXE_Size  : LongInt;             { SIZE OF EXE WHEN SETUP RUN       }
  39.       end;
  40.  
  41.       PSecurity = ^TSecurity;
  42.       TSecurity = Object                 { ERROR CODE, 0 IF NO ERROR       }
  43.         ErrorCode  : Byte;
  44.  
  45.         Constructor Init(FName:TStr12);  { INITIALIZE OBJECT WITH EXE NAME }
  46.         Function    Installed:Boolean;   { DETECTS IF KEY FILE PRESENT     }
  47.         Function    ValidUser:Boolean;   { COMPARES COUNTER W/ MAXTIMES    }
  48.         Function    ErrorStr(Code:Byte):String; { RETURNS THE ERRORSTRING  }
  49.         Procedure   SetUp(CurrCount,MaxCount:Word); { SETS UP THE SECURITY }
  50.         Destructor  Done;                { ALL DONE                        }
  51.  
  52.         PRIVATE
  53.         HiddenFile : PathStr;            { NAME OF HIDDEN FILE             }
  54.         EXEFile    : PathStr;            { NAME OF EXE FILE                }
  55.         SecRec     : TSecureRec;         { SECURITY RECORD                 }
  56.  
  57.         Procedure  ReadFile(Var SecurityRec:TSecureRec);
  58.         Procedure  WriteFile(Var SecurityRec:TSecureRec);
  59.       end;
  60.  
  61. Implementation
  62.  
  63. {---------------------------------------------------------------------------+
  64. | Constructor:  INIT                                                        |
  65. +---------------------------------------------------------------------------+
  66.  
  67. Input:     FName : Name of exe file to secure.
  68. Description:
  69.            Initializes the hidden file name and exe file name.
  70.  
  71. ----------------------------------------------------------------------------}
  72. Constructor TSecurity.Init(FName:TStr12);
  73.  begin
  74.  
  75.  HiddenFile := '-'+#255+#255+#255+#255+#255+#255+'-.-'+#255+'-';
  76.  HiddenFile := FExpand(HiddenFile);
  77.  EXEFile    := FExpand(FName);
  78.  end;
  79.  
  80. Destructor TSecurity.Done;
  81.   begin
  82.   end;
  83.  
  84.  
  85. {---------------------------------------------------------------------------+
  86. | Function:    Installed                                                    |
  87. +---------------------------------------------------------------------------+
  88.  
  89. Returns:  True if hidden file is locatable
  90.  
  91. Description:
  92.           Determines if the key file (hidden file) is available. If not,
  93.           the program should return "key not installed" or "key not found"
  94.           error.
  95. ----------------------------------------------------------------------------}
  96. Function TSecurity.Installed:Boolean;
  97.   Var F : File;
  98.   begin
  99.   Assign(F,HiddenFile);
  100.   SetFAttr(F,Archive);
  101.   Reset(F);
  102.   if IOResult = 0
  103.     then begin
  104.          Installed := True;
  105.          Close(F);
  106.          SetFAttr(F,ReadOnly+Hidden);
  107.          end
  108.     else Installed := False;
  109.   end;
  110.  
  111. {---------------------------------------------------------------------------+
  112. | Procedure:    ReadFile                                                 |
  113. +---------------------------------------------------------------------------+
  114.  
  115. Input:     SecRec : Security record to be read from the Key file.
  116.  
  117. Output:    SecRec : ...
  118.  
  119. Description:
  120.            Reads a file with attributes of Hidden and Read only.
  121. ----------------------------------------------------------------------------}
  122. Procedure TSecurity.ReadFile(Var SecurityRec:TSecureRec);
  123.   Var F:File of TSecureRec;
  124.   begin
  125.   Assign(F,HiddenFile);
  126.   SetFAttr(F,Archive);
  127.   Reset(F);
  128.   Read(F,SecurityRec);
  129.   if IOResult <> 0
  130.     then ErrorCode := 002
  131.     else Close(F);
  132.   end;
  133.  
  134. {---------------------------------------------------------------------------+
  135. | Procedure:    WriteFile                                                   |
  136. +---------------------------------------------------------------------------+
  137.  
  138. Input:    SecRec : Security record to be written to disk.
  139.  
  140. Output:   See input.
  141.  
  142. Description:
  143.           Writes the SecRec to the key file, which is granted the attributes
  144.           of hidden and read-only
  145. ----------------------------------------------------------------------------}
  146. Procedure TSecurity.WriteFile(Var SecurityRec:TSecureRec);
  147.   Var F : File of TSecureRec;
  148.   begin
  149.  
  150.   Assign(F,HiddenFile);
  151.   SetFAttr(F,Archive);
  152.   ReWrite(F);
  153.   Write(F,SecurityRec);
  154.   Close(F);
  155.   if IOResult <> 0
  156.     then ErrorCode := 001;
  157.   SetFAttr(F,ReadOnly+Hidden);
  158.   end;
  159.  
  160. {---------------------------------------------------------------------------+
  161. | Procedure:   SetUp                                                        |
  162. +---------------------------------------------------------------------------+
  163.  
  164. Input:     CurrCount : Count at which user is currently at.  Typically starts
  165.                        at zero, but some of you crazies my want it to vary, to
  166.                        I left it open.
  167.            MaxCount  : Make sure this value is greater then the CurrCount!  Your
  168.                        Program will have a short-life if done otherwise.
  169.                        MaxCount is the maximum number to allow currcout to go to
  170.  
  171. Description:
  172.           Sets up the security program.  WARNING..CAUTION!!! THIS PROCEDURE
  173.           SHOULD BE CALLED WITH CARE.  EVERY CALL TO IT WILL RESET THE
  174.           COUNTS AND ALLOW THE USER ACCESS TO YOUR PROGRAM.
  175. ----------------------------------------------------------------------------}
  176. Procedure TSecurity.SetUp(CurrCount,MaxCount:Word);
  177.   Var FileRec:SearchRec;
  178.   begin
  179.   FindFirst(EXEFile,AnyFile,FileRec);
  180.   if DosError <> 0
  181.     then begin
  182.          ErrorCode := 004;
  183.          Exit;
  184.          end;
  185.   if MaxCount < CurrCount
  186.     then begin
  187.          ErrorCode := 010;
  188.          Exit;
  189.          end;
  190.   SecRec.CurrCount := CurrCount;
  191.   SecRec.MaxCount  := MaxCount;
  192.   SecRec.EXE_Size  := FileRec.Size;
  193.   SecRec.EXE_Time  := FileRec.Time;
  194.   WriteFile(SecRec);
  195.   end; { SetUp }
  196.  
  197.  
  198. {---------------------------------------------------------------------------+
  199. | Function:     ValidUser                                                   |
  200. +---------------------------------------------------------------------------+
  201.  
  202. Returns:   True if user is valid, else returns false.
  203.  
  204. Description:
  205.      Checks Currcount against MaxCount
  206.      Checks for a date or time change in the Executable file (possible
  207.      virus protection).
  208. ----------------------------------------------------------------------------}
  209. Function TSecurity.ValidUser:Boolean;
  210.   Var FileRec : SearchRec;
  211.   begin
  212.   ValidUser := True;
  213.   if Not Installed
  214.     then begin
  215.          ErrorCode := 002;
  216.          ValidUser := False;
  217.          Exit;
  218.          end;
  219.  
  220.   ReadFile(SecRec);
  221.   Inc(SecRec.CurrCount);
  222.   FindFirst(EXEFile,Anyfile,FileRec);
  223.   if SecRec.CurrCount > SecRec.MaxCount then
  224.     begin
  225.     ErrorCode := 003;
  226.     ValidUser := False;
  227.     end
  228.   else
  229.   if (SecRec.EXE_Size <> FileRec.Size) or
  230.      (SecRec.EXE_Time <> FileRec.Time) then
  231.    begin
  232.    ErrorCode := 005;
  233.    ValidUser := False;
  234.    end;
  235.  
  236.   WriteFile(SecRec);
  237.   end; { VALIDUSER }
  238.  
  239.  
  240. {---------------------------------------------------------------------------+
  241. | Function:     TBD                                                         |
  242. +---------------------------------------------------------------------------+
  243.  
  244. Input:     Code : ErrorCode
  245.  
  246. Returns:   String containing error code.
  247.  
  248. Description:
  249.        Returns an error string corresponding to ErrorCode (Code)
  250.  
  251. ----------------------------------------------------------------------------}
  252. Function TSecurity.ErrorStr(Code:Byte):String;
  253.   begin
  254.   Case Code of
  255.     000 : ErrorStr := 'No error(s) encountered.';
  256.     001 : ErrorStr := 'Unable to create necessary key.';
  257.     002 : ErrorStr := 'Unable to find security key.';
  258.     003 : ErrorStr := 'Security time expired.';
  259.     004 : ErrorStr := 'Unable to locate executable file.';
  260.     005 : ErrorStr := 'Executable file has been changed.';
  261.     006 : ErrorStr := 'Key file has been changed.';
  262.     010 : ErrorStr := 'Invalid SetUp parameter value(s).';
  263.   end; { case }
  264.   end;
  265.  
  266. end.
  267. {---------------------------------------------------------------------------}
  268. {//////////////////////////////END OF UNIT//////////////////////////////////}
  269. {---------------------------------------------------------------------------}